<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../stores/userStore'
import { User, Lock, Right } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 表单引用
const loginFormRef = ref()

// 动画状态
const isLoading = ref(false)
const showForm = ref(false)

// 登录处理
const handleLogin = () => {
  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      isLoading.value = true

      // 模拟登录延迟，增强用户体验
      setTimeout(() => {
        // 根据账号密码自动判断用户角色
        if (loginForm.username === 'admin' && loginForm.password === '123456') {
          // 使用Pinia存储用户信息
          userStore.setUserInfo('admin', loginForm.username)
          ElMessage.success('管理员登录成功')
          router.push('/dashboard')
        } else if (loginForm.username === 'visitor' && loginForm.password === '123456') {
          // 使用Pinia存储用户信息
          userStore.setUserInfo('visitor', loginForm.username)
          ElMessage.success('游客登录成功')
          router.push('/dashboard')
        } else {
          ElMessage.error('账号或密码错误')
          isLoading.value = false
        }
      }, 800)
    } else {
      return false
    }
  })
}

// 页面加载动画
onMounted(() => {
  setTimeout(() => {
    showForm.value = true
  }, 300)
})
</script>

<template>
  <div class="login-container">
    <!-- 背景动画元素 -->
    <div class="animated-bg"></div>
    <div class="wave-container">
      <img src="/wave-bg.svg" alt="Wave Background" class="wave-bg" />
    </div>

    <!-- 路标装饰元素 -->
    <div class="road-sign left"></div>
    <div class="road-sign right"></div>

    <!-- 登录框 -->
    <div class="login-box" :class="{ 'show': showForm }">
      <div class="login-header">
        <img src="/highway-logo.svg" alt="Highway Logo" class="logo-image" />
        <h1>高速公路管理系统</h1>
        <div class="subtitle">Highway Management System</div>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="rules"
        label-width="0"
        class="login-form"
      >
        <el-form-item prop="username" class="custom-form-item">
          <div class="input-container">
            <img src="/user-icon.svg" alt="User" class="input-icon" />
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              clearable
              class="custom-input"
            >
            </el-input>
          </div>
        </el-form-item>

        <el-form-item prop="password" class="custom-form-item">
          <div class="input-container">
            <img src="/lock-icon.svg" alt="Lock" class="input-icon" />
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              class="custom-input"
            >
            </el-input>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login-button"
            :loading="isLoading"
            @click="handleLogin"
          >
            <span v-if="!isLoading">登录系统</span>
            <span v-else>登录中...</span>
            <el-icon class="login-icon" v-if="!isLoading"><Right /></el-icon>
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-tips">
        <div class="tips-header">测试账号</div>
        <p><span class="highlight">管理员：</span>admin / 123456</p>
        <p><span class="highlight">游客：</span>visitor / 123456</p>
      </div>

      <div class="login-footer">
        <p>© {{ new Date().getFullYear() }} 高速公路管理系统 版权所有</p>
      </div>
    </div>

    <!-- 底部公路元素 -->
    <div class="highway-bottom">
      <img src="/highway-pattern.svg" alt="Highway Pattern" class="highway-pattern" />
    </div>
  </div>
</template>

<style scoped>
/* 主容器样式 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  background-image: url('/highway-bg.svg');
  background-size: cover;
  background-position: center;
  position: relative;
  overflow: hidden;
}

/* 动画背景 */
.animated-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(ellipse at bottom, #1B2735 0%, #090A0F 100%);
  z-index: -2;
}

/* 波浪背景 */
.wave-container {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: -1;
  overflow: hidden;
}

.wave-bg {
  width: 100%;
  height: auto;
  animation: wave-animation 10s linear infinite;
}

@keyframes wave-animation {
  0% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(-5%);
  }
  100% {
    transform: translateX(0);
  }
}

/* 路标装饰元素 */
.road-sign {
  position: absolute;
  width: 10px;
  height: 120px;
  background: linear-gradient(to bottom, #64B5F6, #1976D2);
  border-radius: 5px;
  z-index: 1;
}

.road-sign::before {
  content: '';
  position: absolute;
  top: 10px;
  left: -15px;
  width: 40px;
  height: 30px;
  background-color: #FFC107;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.road-sign.left {
  left: 15%;
  top: 20%;
  transform: rotate(-5deg);
}

.road-sign.right {
  right: 15%;
  top: 30%;
  transform: rotate(5deg);
}

/* 登录框样式 */
.login-box {
  width: 450px;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transform: translateY(30px);
  opacity: 0;
  transition: all 0.8s ease;
  z-index: 10;
}

.login-box.show {
  transform: translateY(0);
  opacity: 1;
}

/* 登录头部样式 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-image {
  width: 100px;
  height: 100px;
  margin-bottom: 15px;
  animation: pulse 3s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.login-header h1 {
  color: #1565C0;
  font-size: 28px;
  margin-bottom: 5px;
  font-weight: 600;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}

.subtitle {
  color: #5C6BC0;
  font-size: 16px;
  margin-bottom: 20px;
  letter-spacing: 1px;
}

/* 表单样式 */
.login-form {
  margin-bottom: 25px;
}

.custom-form-item {
  margin-bottom: 25px;
}

.input-container {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 12px;
  width: 20px;
  height: 20px;
  z-index: 2;
}

.custom-input :deep(.el-input__wrapper) {
  padding-left: 40px;
  height: 50px;
  width: 368.4px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 15px rgba(25, 118, 210, 0.1);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #1976D2 inset, 0 4px 15px rgba(25, 118, 210, 0.2);
}

/* 登录按钮样式 */
.login-button {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(45deg, #1565C0, #42A5F5);
  border: none;
  border-radius: 8px;
  box-shadow: 0 4px 15px rgba(25, 118, 210, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: all 0.5s ease;
}

.login-button:hover::before {
  left: 100%;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(25, 118, 210, 0.4);
}

.login-icon {
  margin-left: 8px;
  font-size: 16px;
}

/* 提示信息样式 */
.login-tips {
  text-align: center;
  margin-bottom: 20px;
  color: #5C6BC0;
  font-size: 14px;
  background-color: rgba(236, 239, 241, 0.7);
  padding: 15px;
  border-radius: 8px;
}

.tips-header {
  font-weight: 600;
  margin-bottom: 8px;
  color: #1565C0;
}

.login-tips p {
  margin: 5px 0;
}

.highlight {
  color: #1976D2;
  font-weight: 500;
}

/* 页脚样式 */
.login-footer {
  text-align: center;
  color: #5C6BC0;
  font-size: 14px;
  margin-top: 20px;
}

/* 底部公路元素 */
.highway-bottom {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100px;
  overflow: hidden;
  z-index: 1;
}

.highway-pattern {
  width: 200%;
  height: 100px;
  animation: highway-animation 20s linear infinite;
}

@keyframes highway-animation {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(-50%);
  }
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    padding: 30px;
  }

  .road-sign {
    display: none;
  }

  .logo-image {
    width: 80px;
    height: 80px;
  }

  .login-header h1 {
    font-size: 24px;
  }
}
</style>
